﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using PSIEpics;
using string_short;
using System.Threading;
using EpicsJecketClient;
using System.Text.RegularExpressions;

namespace EJClient_tester
{
    public partial class Form1 : Form
    {

        byte[] msg;
        public Form1()
        {
            InitializeComponent();
        }

        EJClient myEJClient;
        private void Form1_Load(object sender, EventArgs e)
        {
            myEJClient = new EJClient("SSFC_01", "test", true, 3);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] a = Regex.Split(textBox1.Text, " ", RegexOptions.None);
            byte[] msg = new byte[a.Length];
            for (int i = 0; i < a.Length; i++)
            {
                msg[i] = (byte)Convert.ToInt32(a[i], 16);
            }

            label1.Text = myEJClient.Msg_send(msg).ToString();
        }

        private void label1_Click(object sender, EventArgs e)
        {
            myEJClient.ClientStatus = 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            myEJClient.Message_Arrive += new EJClient.Message_ArriveHandler(msgArv);    //register message recieve handler
            myEJClient.StartClient();
            label1.Text = myEJClient.ClientStatus.ToString();
            //label2 .Text = myEJClient.getTest();
            
        }

        private void msgArv(object msg1)
        {
            //when recieve message 
            msg = (byte[])msg1;
            MethodInvoker my_fresh = new MethodInvoker(Invoke_fresh);   //have to use methodInvoker to dear eith UI
            this.BeginInvoke(my_fresh);

        }
        private void label2_Click(object sender, EventArgs e)
        {

        }


        private void Invoke_fresh()
        {
            //refresh UI
            label2.Text = "";
            for (int i = 0; i < msg.Length; i++)
            {
                label2.Text +=((byte) msg[i]).ToString("x2")+" ";
            }

        }



    }
}
